package nhatquang.gs.view;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import nhatquang.gs.model.BaseScene;

public class LevelScene extends BaseScene implements IOnSceneTouchListener {

	private int height;
	private int width;;

	private int marginleft;;
	private int margintop;
	private int spacebeetwin;

	private Entity pageOneEtity;
	private Entity backroundEntity;

	private Sprite mSpriteBackGround;

	private Long T1, T2;
	private boolean setTouch;

	private MySpriteMenuItem[][] spriteMenuItem;

	public LevelScene() {

	}

	@Override
	public void createScene() {
		spriteMenuItem = new MySpriteMenuItem[6][3];
		mSpriteBackGround = new Sprite(0, 0,
				mResourcesManager.mTextureRegionBackGroundLevel, VBOM);

		height = 103;
		width = 108;

		T1 = T2 = System.currentTimeMillis();
		setTouch = false;
		marginleft = 63;
		margintop = 53;
		spacebeetwin = 15;
		pageOneEtity = new Entity();
		backroundEntity = new Entity();
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 3; j++) {
				spriteMenuItem[i][j] = new MySpriteMenuItem(getX(j), getY(i),
						width, height,
						getTextTureRegion(mResourcesManager.userData.getLevel(i
								* 3 + j)), mResourcesManager.VBOM, 8);
				pageOneEtity.attachChild(spriteMenuItem[i][j]);
			}
		}
		backroundEntity.attachChild(mSpriteBackGround);
		this.attachChild(backroundEntity);
		this.attachChild(pageOneEtity);
		this.setOnSceneTouchListener(this);
//		attachChild(mSpriteBackGround);

	}

	public float getX(int collumn) {
		return marginleft + collumn * width + spacebeetwin;
	}

	public float getY(int row) {
		return margintop + row * height + spacebeetwin;
	}

	public ITextureRegion getTextTureRegion(int n) {
		switch (n) {
		case 0:
			return mResourcesManager.mTextureRegionZeroStar;
		case 1:
			return mResourcesManager.mTextureRegionOneStar;
		case 2:
			return mResourcesManager.mTextureRegionTowStar;
		case 3:
			return mResourcesManager.mTextureRegionThreeStar;
		default:
			return mResourcesManager.mTextureRegionClockStar;
		}
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		T2 = System.currentTimeMillis();
		if ((T2 - T1) >50 && !setTouch) {
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 3; j++) {
					this.registerTouchArea(spriteMenuItem[i][j]);
				}
			}
			setTouch = true;
		}
		super.onManagedUpdate(pSecondsElapsed);
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		return false;
	}
}
